/********************************************************************************
 * Project Name		[JavaEE_JDBC]
 * File Name     	[UserDaoImpl.java]
 * Creation Date 	[2015-01-01]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ********************************************************************************/
package dao.impl1;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import bean.User;
import dao.UserDao;
import pool.c3p0.JdbcUtils;

/**
 * 使用Connection, PreparedStatement, ResultSet实现的UserDao
 * 
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 * 
 */
public class UserDaoImpl implements UserDao {

	/**
	 * 添加用户
	 */
	@Override
	public boolean addUser(User user) {

		if (this.findUserByName(user.getUsername()) != null) {
			throw new RuntimeException("用户名已存在");
		}

		boolean flag = false;
		Connection connection = null;
		PreparedStatement statement = null;

		String sql = "INSERT INTO user(username, password, age, gender, birthday, image, description) VALUES(?, ?, ?, ?, ?, ?, ?)";
		try {
			connection = JdbcUtils.getInstance().getConnection();
			// connection.setAutoCommit(false);
			statement = connection.prepareStatement(sql);
			statement.setString(1, user.getUsername());
			statement.setString(2, user.getPassword());
			statement.setInt(3, user.getAge());
			statement.setString(4, user.getGender());
			statement.setDate(5, new java.sql.Date(user.getBirthday().getTime()));
			statement.setBinaryStream(6, UserDaoImpl.class.getClassLoader().getResourceAsStream("resources/1.jpg"));
			statement.setString(7, user.getDescription());
			statement.executeUpdate();
			// connection.commit();
			flag = true;
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage(), e);
		} finally {
			JdbcUtils.getInstance().close(null, statement, connection);
		}

		return flag;
	}

	/**
	 * 根据用户名查找用户
	 */
	@Override
	public User findUserByName(String username) {
		User user = null;

		Connection connection = null;
		PreparedStatement statement = null;
		ResultSet resultSet = null;

		try {
			String sql = "SELECT * FROM User WHERE username = ?";
			connection = JdbcUtils.getInstance().getConnection();
			statement = connection.prepareStatement(sql);
			statement.setString(1, username);
			resultSet = statement.executeQuery();
			while (resultSet.next()) {
				user = new User();
				user.setUsername(username);
				user.setPassword(resultSet.getString("password"));
				user.setAge(resultSet.getInt("age"));
				user.setGender(resultSet.getString("gender"));
				user.setBirthday(new java.util.Date(resultSet.getDate("birthday").getTime()));
				user.setImage(resultSet.getBlob("image"));
				user.setDescription(resultSet.getString("description"));
			}
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage(), e);
		} finally {
			JdbcUtils.getInstance().close(resultSet, statement, connection);
		}

		return user;
	}

	/**
	 * 根据用户名更新用户[不更新Blob]
	 */
	@Override
	public boolean updateUserByName(User user, String username) {
		User userFound = this.findUserByName(username);
		if (userFound == null) {
			throw new RuntimeException("要更新的用户不存在");
		}

		boolean flag = false;
		String sql = "UPDATE User SET username = ?, password = ?, age = ?, gender = ?, birthday = ?, description = ? WHERE username = ?";

		Connection connection = null;
		PreparedStatement statement = null;

		try {
			connection = JdbcUtils.getInstance().getConnection();
			statement = connection.prepareStatement(sql);
			statement.setString(1, user.getUsername());
			statement.setString(2, user.getPassword());
			statement.setInt(3, user.getAge());
			statement.setString(4, user.getGender());
			statement.setDate(5, new java.sql.Date(user.getBirthday().getTime()));
			statement.setString(6, user.getDescription());
			statement.setString(7, username);
			statement.executeUpdate();
			flag = true;
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage(), e);
		} finally {
			JdbcUtils.getInstance().close(null, statement, connection);
		}

		return flag;
	}

	/**
	 * 根据用户名删除用户
	 */
	@Override
	public boolean deleteUserByName(String username) {
		User userFound = this.findUserByName(username);
		if (userFound == null) {
			throw new RuntimeException("要删除的用户不存在");
		}

		boolean flag = false;
		String sql = "DELETE FROM User WHERE username = ?";
		Connection connection = null;
		PreparedStatement statement = null;

		try {
			connection = JdbcUtils.getInstance().getConnection();
			statement = connection.prepareStatement(sql);
			statement.setString(1, username);
			statement.executeUpdate();
			flag = true;
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage(), e);
		} finally {
			JdbcUtils.getInstance().close(null, statement, connection);
		}

		return flag;
	}

	/**
	 * 根据用户名和密码登陆
	 */
	@Override
	public boolean login(String username, String password) {
		User user = this.findUserByName(username);
		if (user == null) {
			throw new RuntimeException("用户名不存在");
		} else {
			if (!password.equals(user.getPassword())) {
				throw new RuntimeException("密码不正确");
			}
		}

		return true;
	}
}
